// pages/orderConfirm/orderConfirm.js
import config from '../../config/config';
import {
  OrderService
} from '../../services/order_service';
import {
  ProductService
} from '../../services/product_service';
import wxdata from '../../utils/wxdata';

var photoId;
var selectedCloth;
var selectedColor;
var picId;

Page({
  data: {
    address: "",
    telephone: "",
    username: "",
    remark: "",
    needPrint: false,
    product: {},
    watermarkImage: "",
    requiredFields: {}
  },

  onLoad: function (options) {
    photoId = options.photoId;
    picId = options.picId;
    selectedCloth = options.selectedCloth;
    selectedColor = options.selectedColor;
    let productService = new ProductService();
    productService.getProductById(options.productId).then(res => {
      wxdata.purchase(res.data);
      let product = res.data;
      product.price = options.type == '0' ? (product.price / 100).toFixed(2) : ((product.price + 1500) / 100).toFixed(2);
      this.setData({
        product,
        watermarkImage: options.watermarkImage,
        needPrint: options.type == '0' ? false : true,
      })
    })
  },

  onUsernameChange(event) {
    let value = event.detail.value;
    this.data.username = value;
  },

  onTelephoneChange(event) {
    let value = event.detail.value;
    this.data.telephone = value;
  },

  onRemarkChange(event) {
    let value = event.detail.value;
    this.data.remark = value;
  },

  onFieldChange(event) {
    let key = event.currentTarget.dataset.field;
    let value = event.detail.value;
    this.data.requiredFields[key] = value;
  },

  pay() {
    let valid = true;
    this.data.product.requiredFields.forEach(item => {
      if (item.required && !this.data.requiredFields[item.label]) {
        wx.showToast({
          title: item.label + '不能为空',
          icon: 'none'
        })
        valid = false;
      }
    })
    if (!valid) {
      return;
    }
    let orderService = new OrderService();
    orderService.getOrder({
      productId: this.data.product.id,
      photoId: photoId,
      needPrint: this.data.needPrint,
      address: this.data.needPrint ? {
        receiver: this.data.username,
        receiverPhone: this.data.telephone,
        address: this.data.address
      } : null,
      remark: this.data.remark,
      extraInfo: this.data.requiredFields
    }).then(res => {
      wxdata.requestPay(this.data.product, res.data);
      const orderId = res.data.id;
      const payment = res.data.payment;
      wx.requestPayment({
        ...payment,
        success: async (_) => {
          wxdata.completePay(this.data.product, res.data)
          if (this.data.product.category == 'receipt') {
            wx.requestSubscribeMessage({
              tmplIds: [
                config.templateMessageId
              ],
              fail: (err) => {
                console.log(err);
              }
            });
          }
          wx.reLaunch({
            url: '/pages/orderDetail/orderDetail?id=' + orderId,
          });
        },
        fail: (res) => {
          console.log(res);
        }
      })
    })
  },


  getAddr() {
    wx.chooseAddress({
      success: (result) => {
        console.log(result);
        let address = result.provinceName + " " + result.cityName + " " + result.countyName + result.detailInfo;
        let telephone = result.telNumber;
        let username = result.userName
        this.setData({
          address,
          telephone,
          username
        })
      },
    })
  }
})